package com.das.mechanic_base.widget;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.common.X3SelectCrmQuestionAdapter;
import com.das.mechanic_base.adapter.main.X3CarQuestionHorizontalAdapter;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.bean.main.CarQuestionSelectBean;
import com.das.mechanic_base.bean.main.CrmCarQuetionBean;
import com.das.mechanic_base.utils.LanguageConvent;
import com.das.mechanic_base.utils.PinyinForCarQuestionComparator;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3Utils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class X3CrmCarQuestionSecondDialog extends X3BaseBottomSheetDialog implements View.OnClickListener, X3SelectCrmQuestionAdapter.IOnSelectStyle, X3SlideBar.onTouchLetterListener, X3CarQuestionHorizontalAdapter.CarModleHorDeleteListener {

    private TextView tv_title;
    private TextView tv_cancel;
    private RecyclerView rlv_type;
    private ProgressBar pb_bar;
    private X3SlideBar sb_bar;
    private List<CrmCarQuetionBean.SubItemList> mList = new ArrayList<>();
    private X3SelectCrmQuestionAdapter selectCrmQuestionAdapter;
    private PinyinForCarQuestionComparator pinyinComparator;
    private List<String> headerList;
    private RecyclerView recyclerview_horizontal;
    private X3CarQuestionHorizontalAdapter CarQuestionHorizontalAdapter;
    private Button btn_save;
    private List<CarQuestionSelectBean> selectList;
    private TextView tv_clear;
    private String from;
    private List<CarQuestionSelectBean> selectListsnew;
    private List<CarQuestionSelectBean> selectSingleList = new ArrayList<>();
    private boolean isSelectAll = true;
    private ImageView iv_selectall;
    private String itemName;
    private String itemCode;
    private TextView tv_empty;
    private String title;

    public X3CrmCarQuestionSecondDialog(@NonNull Context context, String from, String title) {
        super(context);
        this.from = from;
        this.title = title;
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    public void initView() {
        tv_title = getView(R.id.tv_title);
//        if(from.equals("1")){//车况问题
//            tv_title.setText(mContext.getString(R.string.car_condition_problems));
//        }else {//保养计划
//            tv_title.setText(mContext.getString(R.string.main_work_plan));
//        }
        tv_title.setText(title == null ? "" : title);
        tv_cancel = getView(R.id.tv_cancel);
        rlv_type = getView(R.id.rlv_type);
        pb_bar = getView(R.id.pb_bar);
        sb_bar = getView(R.id.sb_bar);
        btn_save = getView(R.id.btn_save);
        btn_save.setOnClickListener(this);
        tv_clear = getView(R.id.tv_clear);
        tv_clear.setOnClickListener(this);
        iv_selectall = getView(R.id.iv_selectall);
        iv_selectall.setOnClickListener(this);
        tv_empty = getView(R.id.tv_empty);
        recyclerview_horizontal = getView(R.id.recyclerview_horizontal);
        //水平方向
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false);
        recyclerview_horizontal.setLayoutManager(linearLayoutManager);
        CarQuestionHorizontalAdapter = new X3CarQuestionHorizontalAdapter(mContext);
        recyclerview_horizontal.setAdapter(CarQuestionHorizontalAdapter);
        CarQuestionHorizontalAdapter.setiOnDeleteHor(this);

        tv_cancel.setOnClickListener(this);
        pinyinComparator = new PinyinForCarQuestionComparator();

        sb_bar.setOnTouchLetterListener(this);
        rlv_type.setLayoutManager(new LinearLayoutManager(mContext));
        rlv_type.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                LinearLayoutManager layoutManager = (LinearLayoutManager) rlv_type.getLayoutManager();
                int itemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
                if (itemPosition < 0 || itemPosition >= mList.size()) {
                    return;
                }
                String sortLetters = mList.get(itemPosition).sortLetters;
                if (X3StringUtils.isEmpty(sortLetters)) {
                    return;
                }
                for (int i = 0; i < headerList.size(); i++) {
                    if (sortLetters.equalsIgnoreCase(headerList.get(i))) {
                        if (sb_bar != null) {
                            sb_bar.changeSelectIndex(i);
                        }
                        break;
                    }
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_carm_car_model;
    }

    public void changeData( List<CrmCarQuetionBean.SubItemList> dataLists, String itemCode, String itemName, List<CarQuestionSelectBean> selectLists) {
        selectSingleList.clear();
        selectList = selectLists;
        selectListsnew = new ArrayList<>();
        selectListsnew.clear();
        selectListsnew.addAll(selectList);
        this.itemName = itemName;
        this.itemCode = itemCode;
        pb_bar.setVisibility(View.GONE);
        if(X3StringUtils.isListEmpty(dataLists)){
            tv_empty.setVisibility(View.VISIBLE);
            if(from.equals("1")){
                tv_empty.setText(String.format(mContext.getResources().getString(R.string.no_condition_question),itemName));
            }else {
                tv_empty.setText(String.format(mContext.getResources().getString(R.string.no_maintance_plan),itemName));
            }
            rlv_type.setVisibility(View.GONE);
            sb_bar.setVisibility(View.GONE);
        }else {
            tv_empty.setVisibility(View.GONE);
            rlv_type.setVisibility(View.VISIBLE);
            sb_bar.setVisibility(View.VISIBLE);
            this.mList = dataLists;
    //        tv_title.setText(title);
            mList = LanguageConvent.filledDatas(dataLists);
            /**
             * 对数据进行A-Z排序
             */
            Collections.sort(mList,pinyinComparator);

            headerList = new ArrayList<>();


            for (CrmCarQuetionBean.SubItemList subItemList : mList) {
                headerList.add(subItemList.sortLetters);
            }

            Set<String> set = new LinkedHashSet<>(headerList);
            headerList = new ArrayList<>(set);
            if (sb_bar != null) {
                sb_bar.changeNum(headerList);
            }

            for (int i = 0; i < dataLists.size(); i++) {   //  1  2  3     1 2 3 4    记录索引
                for (int j = 0; j < selectList.size(); j++){
                    if(selectList.get(j).itemCode.equals(itemCode) && selectList.get(j).itemCodes.equals(dataLists.get(i).itemCode)){//相等时的索引   非全选
                        selectSingleList.add(selectList.get(j));
                    }else if(selectList.get(j).itemCode.equals(itemCode) && selectList.get(j).itemCodes.equals("ALL")){//全选
                        //拼接当前选中的全部数据 加入到selectSingleList
                        CarQuestionSelectBean carQuestionSelectBean = new CarQuestionSelectBean();
                        carQuestionSelectBean.itemName = itemName;
                        carQuestionSelectBean.itemCode = itemCode;
                        carQuestionSelectBean.itemNames = dataLists.get(i).itemName;
                        carQuestionSelectBean.itemCodes = dataLists.get(i).itemCode;
                        selectSingleList.add(carQuestionSelectBean);
                    }
                }
            }

            if(selectSingleList.size() == dataLists.size()){
                isSelectAll = false;
                iv_selectall.setImageResource(R.mipmap.x3_selectall);
            }else {
                isSelectAll = true;
                iv_selectall.setImageResource(R.mipmap.x3_clearselectall);
            }

            if(CarQuestionHorizontalAdapter != null){
                CarQuestionHorizontalAdapter.setDataList(selectListsnew,selectSingleList,itemName,itemCode);
            }
            rlv_type.addItemDecoration(new X3UserItemDecoration());
            selectCrmQuestionAdapter = new X3SelectCrmQuestionAdapter(mContext,itemCode,itemName);
            rlv_type.setAdapter(selectCrmQuestionAdapter);
            selectCrmQuestionAdapter.changeData(mList,selectListsnew);
            selectCrmQuestionAdapter.setiOnSelectStyle(this);
            }
    }

    @Override
    public void show() {
        super.show();
        rlv_type.setVisibility(View.GONE);
        pb_bar.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_cancel) {
            dismiss();
        } else if (id == R.id.btn_save) {//保存
            if (!X3Utils.isFastClick()) {
                return;
            }
            if (iOnClick != null) {
                iOnClick.iOnCarModelSave(selectListsnew);
            }
            dismiss();
        } else if (id == R.id.tv_clear) {//清空全部
            if (!X3Utils.isFastClick()) {
                return;
            }
            selectListsnew.clear();
            CarQuestionHorizontalAdapter.setDataList(selectListsnew, selectSingleList, itemName, itemCode);
            selectCrmQuestionAdapter.changeData(mList, selectListsnew);
            if (iOnClick != null) {
                iOnClick.iOnCarModelClear(selectListsnew);
            }
        } else if (id == R.id.iv_selectall) {//全选
            if (selectCrmQuestionAdapter != null) {
                selectCrmQuestionAdapter.isSelelctAll(isSelectAll);

            }
        }
    }

    IOnClickCar iOnClick;
    public void setiOnClick(IOnClickCar iOnClick) {
        this.iOnClick = iOnClick;
    }
    public interface IOnClickCar {
        void iOnCarModelSave(List<CarQuestionSelectBean> selectList);
        void iOnCarModelClear(List<CarQuestionSelectBean> selectList);
    }
    @Override
    public void onTouchLetterChange(String letter, boolean touch) {
        for (int i = 0; i < mList.size(); i++) {
            if (letter.equalsIgnoreCase(mList.get(i).sortLetters)) {
                ((LinearLayoutManager)rlv_type.getLayoutManager()).scrollToPositionWithOffset(i,0);
                break;
            }
        }
    }
    @Override
    public void iOnCarModelIsSelect(List<CarQuestionSelectBean> selectListAll, boolean isAdd,String type,String itemName,String itemCode) {
        if (isAdd) {
            if(type.equals("all")){//全部选中
//                selectSingleList.clear();
                selectSingleList.addAll(selectListAll);
                CarQuestionSelectBean carQuestionSelectBean = new CarQuestionSelectBean();
                carQuestionSelectBean.itemName = itemName;
                carQuestionSelectBean.itemCode = itemCode;
                carQuestionSelectBean.itemCodes = "ALL";
                carQuestionSelectBean.itemNames = "";
                selectListsnew.add(carQuestionSelectBean);
                //移除掉之前选中的
                for (CarQuestionSelectBean selectBean : selectSingleList) {
                    if (selectBean == null) {
                        continue;
                    }
                    Iterator<CarQuestionSelectBean> iterator = selectListsnew.iterator();
                    while (iterator.hasNext()) {
                        CarQuestionSelectBean next = iterator.next();
                        if (next == null) {
                            continue;
                        }
                        if (X3StringUtils.isEmpty(next.itemCodes)) {
                            continue;
                        }
                        if (next.itemCode.equals(itemCode) && next.itemCodes.equals(selectBean.itemCodes)) {
                            iterator.remove();
                        }
                    }
                }
                isSelectAll = !isSelectAll;
                iv_selectall.setImageResource(isSelectAll ? R.mipmap.x3_clearselectall : R.mipmap.x3_selectall);

            }else{//一个一个加的

                selectSingleList.addAll(selectListAll);//selectSingleList 单选添加的

                if(selectSingleList.size() == mList.size()){//全部选中了
                    //移除掉全部  换 All
                    for (CarQuestionSelectBean selectBean : selectSingleList){
                        if (selectBean == null) {
                            continue;
                        }
                        Iterator<CarQuestionSelectBean> iterator = selectListsnew.iterator();
                        while (iterator.hasNext()) {
                            CarQuestionSelectBean next = iterator.next();
                            if (next == null) {
                                continue;
                            }
                            if (X3StringUtils.isEmpty(next.itemCodes)) {
                                continue;
                            }
                            if (next.itemCodes.equals(selectBean.itemCodes)) {
                                iterator.remove();
                            }
                        }
                    }
                    CarQuestionSelectBean carQuestionSelectBean = new CarQuestionSelectBean();
                    carQuestionSelectBean.itemName = itemName;
                    carQuestionSelectBean.itemCode = itemCode;
                    carQuestionSelectBean.itemCodes = "ALL";
                    carQuestionSelectBean.itemNames = "";
                    selectListsnew.add(carQuestionSelectBean);
                    isSelectAll = false;
                    iv_selectall.setImageResource( R.mipmap.x3_selectall);
                }else {//没全部选中
                    selectListsnew.addAll(selectListAll);
                    isSelectAll = true;
                    iv_selectall.setImageResource( R.mipmap.x3_clearselectall);
                }

            }
        }else {//取消一个或者全部取消
            iv_selectall.setImageResource( R.mipmap.x3_clearselectall);
            isSelectAll = true;
            for (CarQuestionSelectBean selectBean : selectListAll) {
                if (selectBean == null) {
                    continue;
                }
                Iterator<CarQuestionSelectBean> iterator = selectSingleList.iterator();
                while (iterator.hasNext()) {
                    CarQuestionSelectBean next = iterator.next();
                    if (next == null) {
                        continue;
                    }
                    if (X3StringUtils.isEmpty(next.itemCodes)) {
                        continue;
                    }
                    if (next.itemCode.equals(itemCode) && next.itemCodes.equals(selectBean.itemCodes)) {
                        iterator.remove();
                    }
                }
            }
            for (int i = 0; i < selectListsnew.size(); i++){
                if(selectListsnew.get(i).itemCode.equals(itemCode) && selectListsnew.get(i).itemCodes.equals("ALL")){
                    selectListsnew.addAll(selectSingleList);
                }
            }
            //ALL移除掉
            for (CarQuestionSelectBean selectBean : selectListAll) {
                if (selectBean == null) {
                    continue;
                }
                Iterator<CarQuestionSelectBean> iterator = selectListsnew.iterator();
                while (iterator.hasNext()) {
                    CarQuestionSelectBean next = iterator.next();
                    if (next == null) {
                        continue;
                    }
                    if (X3StringUtils.isEmpty(next.itemCodes)) {
                        continue;
                    }
                    if (next.itemName.equals(itemName) && next.itemCodes.equals("ALL")) {
                        iterator.remove();
                    } else if (next.itemCode.equals(itemCode) && next.itemCodes.equals(selectBean.itemCodes)) {
                        iterator.remove();
                    }
                }
            }
        }
        if (CarQuestionHorizontalAdapter != null) {
            CarQuestionHorizontalAdapter.setDataList(selectListsnew,selectSingleList,itemName,itemCode);
        }
    }
    @Override
    public void deleteHorModel() {//水平列表删除数据后 重新刷新车款
        selectCrmQuestionAdapter.changeData(mList, selectListsnew);
        if(selectListsnew.contains(mList)){
            isSelectAll = false;
            iv_selectall.setImageResource(R.mipmap.x3_selectall);
        }else {
            isSelectAll = true;
            iv_selectall.setImageResource(R.mipmap.x3_clearselectall);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("机会列表车况问题二级弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("机会列表车况问题二级弹窗");
    }
}
